// pages/module/order/paySuccess/paySuccess.js
import regeneratorRuntime from '../../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../../utils/storage.js';
import {
  fetchOpenId,
} from '../../../../utils/imapi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    networkType: 1,
    show: 0,
    cont: 0,
    data: '',
    success: '-1',
    top_order_no: '',
    mobile: '',
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '支付中'
    })
    var timer = '';
    var that = this;
    var url = 'api/shopCart/checkPayStatus';
    var data = {
      top_order_no: options.top_order_no
    };
    that.setData({
      top_order_no: options.top_order_no
    })
    timer = setInterval(function () {
      req.get(that, url, data,
        function (res) {
          if (res.data.returnCode == 0) {
            clearInterval(timer);
            wx.hideLoading();
            that.setData({
              show: 1,
              success: 1,
              data: res.data.data,
            });
          } else {
            that.data.cont++;
            if (that.data.cont >= 5) {
              wx.hideLoading();
              wx.showToast({
                title: '支付失败'
              })
              clearInterval(timer);
              that.setData({
                mobile: res.data.data.customer_phone,
                show: 1,
                success: 0,
              });
              return;
            }
          }

        },
        function (res) {
          clearInterval(timer)
        });
    }, 1000);
  },

  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.mobile
    })
  },

  backIndex() {
    getApp().globalData.orderDetail = 0;
    wx.switchTab({
      url: '/pages/tabbar/index/index',
    })
  },

  lookOrder() {
    getApp().globalData.orderDetail = 0;
    wx.redirectTo({
      url: '/pages/module/order/myOrder/myOrder?status=0',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})